import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeSet;

public class Solution1187 {
    public int makeArrayIncreasing(int[] arr1, int[] arr2) {
        int MAX=Integer.MAX_VALUE/2;
        TreeSet<Integer> treeSet=new TreeSet<>();
        for (int i : arr1) {
            treeSet.add(i);
        }
        for (int i : arr2) {
            treeSet.add(i);
        }
        HashMap<Integer,Integer> map=new HashMap<>();
        int count=0;
        for (Integer integer : treeSet) {
            count++;
            map.put(integer,count);
        }
        TreeSet<Integer> set=new TreeSet<>();
        for (int i : arr2) {
            set.add(map.get(i));
        }
        int[][] f=new int[arr1.length][count+1];
        for (int i=0;i<arr1.length;i++){
            Arrays.fill(f[i],MAX);
        }
        for (int i=0;i<arr1.length;i++){
            if (i==0){
                f[0][map.get(arr1[0])]=0;
                for (Integer integer : set) {
                    f[0][integer]=Math.min(f[0][integer],1);
                }
            }else {
                int min=MAX;
                for (int j=1;j<=count;j++){
                    if (j<map.get(arr1[i])) {
                        min=Math.min(min,f[i-1][j]);
                    }
                }
                f[i][map.get(arr1[i])]=min;
                min=MAX;
                for (int j=1;j<=count;j++){
                    min=Math.min(min,f[i-1][j-1]+1);
                    if (set.contains(j)){
                        f[i][j]=Math.min(f[i][j],min);
                    }
                }
            }
        }
        int min=MAX;
        for (int i=1;i<=count;i++){
            if (f[arr1.length-1][i]<min){
                min=f[arr1.length-1][i];
            }
        }
        if (min==MAX){
            return -1;
        }else {
            return min;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution1187().makeArrayIncreasing(new int[]{1,5,3,6,7},new int[]{1,6,3,3}));
    }
}
